package com.fingard.dsp.bank.directbank.cbhb01;

import java.util.Date;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

public class CBHB01Base extends DirectBase implements ITcpClient {

    protected String getCharset() {
        return getBankConfig().getCharset("UTF-8");
    }

    protected String getQryTransID() {
        Date tmpDateNow = new Date();
        return Format.Time12Format.format(tmpDateNow);
    }

    protected void writeCommonHeader(XmlTextWriter xmlWriter, CBHB01Header p_bankHeader) throws Exception {
        if (StringHelper.isNullOrEmpty(p_bankHeader.TransCode)) {
            throw new Exception("未赋值TransCode");
        }
        if (StringHelper.isNullOrEmpty(p_bankHeader.ClientSeqno)) {
            throw new Exception("未赋值ClientSeqno");
        }
        xmlWriter.writeStartDocument();
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("CBHB");
        xmlWriter.writeStartElement("head");
        xmlWriter.writeElementString("Locale", "zh_CN");//地区
        if (p_bankHeader.RequestTime == null) {
            p_bankHeader.RequestTime = new Date();
        }
        xmlWriter.writeElementString("RequestTime", Format.DateTime14Format.format(p_bankHeader.RequestTime));//请求时间
        xmlWriter.writeElementClosed("RespondTime");//响应时间
        xmlWriter.writeElementString("UserId", bankFront.operator);//用户ID
        xmlWriter.writeElementString("CifNo", bankFront.org);//企业客户号
        xmlWriter.writeElementString("TransCode", p_bankHeader.TransCode);//交易码
        xmlWriter.writeElementString("ClientSeqno", p_bankHeader.ClientSeqno);//客户交易流水号
        xmlWriter.writeElementClosed("RecordIndex");//起始记录下标
        xmlWriter.writeElementClosed("RecordMax");//最大查询数
        xmlWriter.writeElementClosed("BankSeqno");//银行交易流水号
        xmlWriter.writeElementClosed("RespondCode");//返回码
        xmlWriter.writeElementClosed("RespondInfo");//返回消息
        xmlWriter.writeElementClosed("RecordTotal");//总记录数
        xmlWriter.writeElementClosed("RecordCount");//返回记录数
        xmlWriter.writeElementClosed("TransPatches");//保留
        xmlWriter.writeEndElement();
    }

    //分页查询
    protected void writeCommonHeader(XmlTextWriter xmlWriter, CBHB01Header p_bankHeader,String startIndex,String pageSize) throws Exception {
        if (StringHelper.isNullOrEmpty(p_bankHeader.TransCode)) {
            throw new Exception("未赋值TransCode");
        }
        if (StringHelper.isNullOrEmpty(p_bankHeader.ClientSeqno)) {
            throw new Exception("未赋值ClientSeqno");
        }
        xmlWriter.writeStartDocument();
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("CBHB");
        xmlWriter.writeStartElement("head");
        xmlWriter.writeElementString("Locale", "zh_CN");//地区
        if (p_bankHeader.RequestTime == null) {
            p_bankHeader.RequestTime = new Date();
        }
        xmlWriter.writeElementString("RequestTime", Format.DateTime14Format.format(p_bankHeader.RequestTime));//请求时间
        xmlWriter.writeElementClosed("RespondTime");//响应时间
        xmlWriter.writeElementString("UserId", bankFront.operator);//用户ID
        xmlWriter.writeElementString("CifNo", bankFront.org);//企业客户号
        xmlWriter.writeElementString("TransCode", p_bankHeader.TransCode);//交易码
        xmlWriter.writeElementString("ClientSeqno", p_bankHeader.ClientSeqno);//客户交易流水号
        xmlWriter.writeElementString("RecordIndex",startIndex);//起始记录下标
        xmlWriter.writeElementString("RecordMax",pageSize);//最大查询数
        xmlWriter.writeElementClosed("BankSeqno");//银行交易流水号
        xmlWriter.writeElementClosed("RespondCode");//返回码
        xmlWriter.writeElementClosed("RespondInfo");//返回消息
        xmlWriter.writeElementClosed("RecordTotal");//总记录数
        xmlWriter.writeElementClosed("RecordCount");//返回记录数
        xmlWriter.writeElementClosed("TransPatches");//保留
        xmlWriter.writeEndElement();
    }

    protected String[] sendToBank(String p_reqStr) throws Exception {

        String[] retStr = new String[]{"", ""};

        WriteBankLogLn("2.提交银行的地址：" + bankFront.serverIP + ":" + bankFront.serverPort);

        //发送银行前置
        TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP, Integer.parseInt(bankFront.serverPort), this);
        String tmpCharset = getCharset();
        p_reqStr += "\r\n";
        byte[] tmpBytesSend = p_reqStr.getBytes(tmpCharset);
        WriteBankLogStep2(p_reqStr);
        TcpReceiver tmpReceiver = tmpClient.sendMessage(tmpBytesSend);
        if (StringHelper.isNullOrEmpty(tmpReceiver.connState)) {
            String tmpBkStr = new String(tmpReceiver.recBytes, 0, tmpReceiver.readedLength, tmpCharset);
            WriteBankLogStep3(tmpBkStr);
        }
        retStr[0] = tmpReceiver.connState;
        if (tmpReceiver.isCompleted) {
            retStr[1] = new String(tmpReceiver.recBytes, tmpReceiver.contentStartIndex, tmpReceiver.contentLength, tmpCharset);
        } else {
            retStr[1] = tmpReceiver.getConnMsg();
            WriteBankLogLn(retStr[1]);
        }
        return retStr;
    }

    //上送格式为json格式
    protected String[] sendToATS(String jsonData) throws Exception {
        String[] retStr = new String[]{"", ""};
        WriteBankLogLn("2.提交ATS的地址：" + FGBiz.dspCenter.dspSet.receiptPushServerUrl);


        //发送银行前置
        WebRequest tmpRequest = new WebRequest(FGBiz.dspCenter.dspSet.receiptPushServerUrl);
        tmpRequest.setConnectTimeout(60000);
        tmpRequest.setRequestProperty("connection", "Close");
        tmpRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        String charsetName = "GBK";
        WriteBankLogLn("3.推送给ATS的json数据" + jsonData);
        byte[] tmpWriteBytes = jsonData.getBytes(charsetName);
        retStr = tmpRequest.upload(tmpWriteBytes, charsetName);
        WriteBankLogLn("4.返回的数据" + retStr[1]);

        return retStr;
    }


    public void getContentLength(TcpReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1) {
            String tmpCharset = getCharset();
            String tmpRecNow = new String(p_receiver.recBytes, 0, p_receiver.readedLength, tmpCharset);
            if (tmpRecNow.indexOf("</CBHB>") >= 0) {
                p_receiver.contentLength = p_receiver.readedLength;
                p_receiver.contentStartIndex = 0;
            }
        }
    }
}
